<template>
  <q-dialog class="my-dialog-form" v-model="isFlagShow">
    <q-card class="my-card">
      <q-toolbar class="row items-center my-form-header">
        <div>
          {{dlgTitle}}
        </div>
        <q-space />
        <q-btn flat round dense icon="mdi-close" @click="close"/>
      </q-toolbar>
      <q-separator />
      <q-card-setion class="q-mx-sm">
        <q-scroll-area class="my-form-container">
          <table class="my-form-table">
            <tr class="my-row-height">
              <td class="my-label"><span style="color: red">**</span>设备型号：</td>
              <td class="my-value">
                <q-select
                  dense
                  outlined
                  options-dense
                  :options="devTypeArr"
                  v-model="param.type"
                  option-value="label"
                  emit-value
                  options-selected-class="teal"
                  @blur="v$.type.$touch()"
                  :error="v$.type.$error"
                  error-message="请选择控制器型号！"
                />
              </td>
              <td class="my-label"><span style="color: red">**</span>设备名称：</td>
              <td class="my-value">
                <q-input
                  outlined
                  dense
                  v-model="param.cname"
                  @blur="v$.cname.$touch()"
                  :error="v$.cname.$error"
                  error-message="请输入不少于2个字符！"
                />
              </td>
            </tr>
            <tr class="my-row-height">
              <td class="my-label"><span style="color: red">**</span>通信地址：</td>
              <td class="my-value">
                <q-input
                  outlined
                  dense
                  v-model="param.commAddr"
                  @blur="v$.commAddr.$touch()"
                  :error="v$.commAddr.$error"
                  error-message="请输入不少于2个字符！"
                />
              </td>
              <td class="my-label"><span style="color: red">**</span>通信端口：</td>
              <td class="my-value">
                <q-input
                  outlined
                  dense
                  type="number"
                  v-model.lazy.number="param.commPort"
                  @blur="v$.commPort.$touch()"
                  :error="v$.commPort.$error"
                  error-message="请输入不少于2个字符！"
                />
              </td>
            </tr>
            <tr class="my-row-height">
              <td class="my-label"><span style="color: red">**</span>项目名称：</td>
              <td class="my-value">
                <q-select
                  dense
                  outlined
                  options-dense
                  :options="projectArr"
                  v-model="param.project"
                  option-value="label"
                  emit-value
                  options-selected-class="teal"
                  @blur="v$.project.$touch()"
                  :error="v$.project.$error"
                  error-message="请选择项目名称！"
                />
              </td>
              <td class="my-label"><span style="color: red">**</span>排序序码：</td>
              <td class="my-value">
                <q-input
                  outlined
                  dense
                  v-model="param.sortCode"
                  @blur="v$.sortCode.$touch()"
                  :error="v$.sortCode.$error"
                  error-message="请输入不少于2个字符！"
                />
              </td>
            </tr>
            <tr class="my-row-height">
              <td class="my-label">联网账号：</td>
              <td class="my-value">
                <q-input
                  outlined
                  dense
                  v-model="param.admin"
                />
              </td>
              <td class="my-label">联网密码：</td>
              <td class="my-value">
                <q-input
                  outlined
                  dense
                  v-model="param.password"
                />
              </td>
            </tr>
            <tr class="my-row-height">
              <td class="my-label">加密算法：</td>
              <td class="my-value">
                <q-select
                  dense
                  outlined
                  options-dense
                  :options="encryptionTypeArr"
                  v-model="param.encryption"
                  option-value="label"
                  emit-value
                  options-selected-class="teal"
                />
              </td>
              <td class="my-label">通信协议：</td>
              <td class="my-value">
                <q-select
                  dense
                  outlined
                  options-dense
                  :options="protocolTypeArr"
                  v-model="param.protocol"
                  option-value="label"
                  emit-value
                  options-selected-class="teal"
                />
              </td>
            </tr>
          </table>
        </q-scroll-area>
      </q-card-setion>
      <q-separator />
      <div class="q-gutter-sm my-form-footer">
        <q-btn
          unelevated
          dense
          label="提交"
          type="submit"
          color="teal-3"
          @click="onSubmit"
          style="width: 80px"
        />
        <q-btn
          unelevated
          dense
          label="重置"
          type="reset"
          color="orange-3"
          @click="onReset"
          style="width: 80px"
        />
      </div>
    </q-card>
  </q-dialog>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref, toRef, toRefs, PropType, watch } from 'vue';
import { isObjectEmpty } from '../../../util/utils';
import { required, minLength } from '@vuelidate/validators'
import { useVuelidate } from '@vuelidate/core';
import { cataItem } from '../../../hook/common/useCatalogType';
import { useCatalogTool } from 'src/hook/common/useCatalogTool';

import { DeviceItem, DeviceItemVO } from './DeviceModel';

export default defineComponent({
  cname: 'DeviceDlg',
  components: {

  },
  props: {
    itemData: {
      type: Object as PropType<DeviceItem>,
      required: true
    },
    devTypeArr: {
      type: Array as PropType<cataItem[]>,
      required: true
    },
    projectArr: {
      type: Array as PropType<cataItem[]>,
      required: true
    },
    protocolTypeArr: {
      type: Array as PropType<cataItem[]>,
      required: true
    },
    encryptionTypeArr: {
      type: Array as PropType<cataItem[]>,
      required: true
    }
  },
  emits: ['submit-device-item', 'cancel-device-item'],
  setup (props, context) {
    //数据解构
    const {
      itemData,
      devTypeArr,
      projectArr,
      protocolTypeArr,
      encryptionTypeArr
    } = toRefs(props);

    //*******************************
    //      目录操作
    //*******************************
    const {
      findCatalogLabel,
      findCatalogId
    } = useCatalogTool();

    //*******************************
    //      表单项
    //*******************************
    //窗口显示
    let isFlagShow = ref(false);

    //窗口标题
    let dlgTitle = ref('');

    //界面数据
    let param = ref<DeviceItemVO>({
      id: 0,
      type: '',
      cname: '',
      commAddr: '',
      commPort: 0,
      admin: '',
      password: '',
      encryption: '',
      protocol: '',
      project: '',
      country: '',
      province: '',
      sortCode: ''
    });

    //关闭窗口
    const close = () => {
      isFlagShow.value = false;
      context.emit('cancel-device-item');
    }

    //显示窗口
    const show = (text: string) => {
      isFlagShow.value = true;
      dlgTitle.value = text;
    }

    //提交窗口
    const onSubmit = () => {
      v$.value.$validate();
      if (v$.value.$error) {
        alert('输入参数校验错误!');
      } else {
        isFlagShow.value = false;
        let item: DeviceItem = {
          id: param.value.id,
          typeId: findCatalogId(devTypeArr.value, param.value.type),
          cname: param.value.cname,
          commAddr: param.value.commAddr,
          commPort: param.value.commPort,
          admin: param.value.admin,
          password: param.value.password,
          encryptionId: findCatalogId(encryptionTypeArr.value, param.value.encryption),
          protocolId: findCatalogId(protocolTypeArr.value, param.value.protocol),
          projectId: findCatalogId(projectArr.value, param.value.project),
          countryId: 0,
          provinceId: 0,
          sortCode: param.value.sortCode,
          createTime: '',
          createUserId: 0,
          updateTime: '',
          updateUserId: 0
        };

        //发送消息
        context.emit('submit-device-item', item);
      }
    }

    //重置
    const onReset = () => {
      if (param.value) {
        param.value.type = '';
        param.value.cname = '';
        param.value.commAddr = '';
        param.value.commPort = 0;
        param.value.admin = '';
        param.value.password = '';
        param.value.encryption = '';
        param.value.protocol = '';
        param.value.project = '';
        param.value.country = '';
        param.value.province = '';
        param.value.sortCode = '';
      }

      //表单验证初始化
      v$.value.$reset();
    }

    //*******************************
    //      表单校验
    //*******************************
    //配置校验规则
    const rules = {
      id: {},
      type: { required },
      cname: { required, minLength: minLength(2) },
      commAddr: { required },
      commPort: { required },
      admin: {},
      password: {},
      encryption: {},
      protocol: {},
      project: { required },
      country: {},
      province: {},
      sortCode: { required }
    }

    //使用vuelidate，建立变量对应的校验规则
    const v$ = useVuelidate(rules, {
      id: toRef(param.value, 'id'),
      type: toRef(param.value, 'type'),
      cname: toRef(param.value, 'cname'),
      commAddr: toRef(param.value, 'commAddr'),
      commPort: toRef(param.value, 'commPort'),
      admin: toRef(param.value, 'admin'),
      password: toRef(param.value, 'password'),
      encryption: toRef(param.value, 'encryption'),
      protocol: toRef(param.value, 'protocol'),
      project: toRef(param.value, 'project'),
      country: toRef(param.value, 'country'),
      province: toRef(param.value, 'province'),
      sortCode: toRef(param.value, 'sortCode')
    });


    //*******************************
    //      启动模块
    //*******************************
    //初始化
    const init = () => {
      if (!isObjectEmpty(itemData.value)) {
        param.value.id = itemData.value.id;
        param.value.type = findCatalogLabel(devTypeArr.value, itemData.value.typeId);
        param.value.cname = itemData.value.cname;
        param.value.commAddr = itemData.value.commAddr;
        param.value.commPort = itemData.value.commPort;
        param.value.admin = itemData.value.admin;
        param.value.password = itemData.value.password;
        param.value.encryption = findCatalogLabel(encryptionTypeArr.value, itemData.value.encryptionId);
        param.value.protocol = findCatalogLabel(protocolTypeArr.value, itemData.value.protocolId);
        param.value.project = findCatalogLabel(projectArr.value, itemData.value.projectId);
        param.value.country = '';
        param.value.province = '';
        param.value.sortCode = itemData.value.sortCode;
      }

      //表单验证初始化
      v$.value.$reset();
    }

    //监听对象的所有属性
    watch(() => itemData, () => {
      init();
    }, { deep: true });

    //启动挂载组件
    onMounted(() => {
      init();
    })

    return {
      itemData,
      devTypeArr,
      projectArr,
      protocolTypeArr,
      encryptionTypeArr,

      isFlagShow,
      dlgTitle,
      param,
      init,
      close,
      show,
      onSubmit,
      onReset,
      v$
    }
  }
})
</script>

<style lang="scss" scoped>
.my-card {
  alignment: center;
  width: 700px;
  max-width: 900px;
  .my-form-container {
    text-align: center;
    padding: 5px 50px 5px 50px;
    height: 400px;
    .my-form-table {
      td {
        text-align: right;
        width: 140px;
        padding-bottom: 4px;
      }
    }
  }
}
</style>
